<template>
  <div>
    <div class="back">
      <el-form
        class="form"
        label-position="left"
        :model="form"
        :rules="rules"
        ref="form"
      >
        <p class="title">后台管理登录</p>
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入管理员账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="upwd">
          <el-input
            v-model="form.upwd"
            type="password"
            placeholder="请输入管理员密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <br>
          <el-button type="primary" style="width: 100%" @click="onSubmit('form')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import httpApi from '@/http';
export default {
  data() {
    return {
      form: {
        phone: "",
        upwd: "",
      },
      rules: {
        phone: [
          { required: true, message: "请填写注册电话号", trigger: "blur" },
          // {
          //   pattern: /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/,
          //   message: "请输入正确电话号",
          //   trigger: "blur",
          // },
        ],
        upwd: [
          { required: true, message: "请填写密码", trigger: "blur" },
          // {
          //   pattern: /^\w{6,15}$/,
          //   message: "6~15位字符，可以包含：数字、字母、下划线",
          //   trigger: "blur",
          // },
        ],
      },
    };
  },

  methods: {
    onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            httpApi.adminApi.login(this.form).then( res =>{
              console.log(res);
              if(res.data.code == 202){
                this.$message.error('登录错误，该账号为前台用户，请前往前台用户登录');
              }else if(res.data.code == 200){
                  // 登录成功后，需要将用户对象存入vuex
                  // mutations中定义了一个方法：updateUserInfo()用于更新用户信息
                  // 在此需要让vuex执行该方法，更新state中的user对象
                  // this.$store.commit('updateUserInfo', res.data.data.user)
                  // this.$store.commit('updateToken', res.data.data.token)
                  console.log(res.data.results.user_type,"身份");
                this.$store.commit('updataType', res.data.results.user_type)
                this.$store.commit("updataName", res.data.results.uname);
                this.$store.commit("updataid", res.data.results.uid);
                // this.$router.push('/home/index')

                if(res.data.results.user_type == 1){
                  this.$router.push('/home/shop-list')
                }else if(res.data.results.user_type == 2){
                  this.$router.push(`/home/shop-details/${this.$store.state.uid}`)
                }else{
                  this.$message.error('身份无法确定');
                }
  
              }else{
                this.$message.error('登录失败');
              }
              this.$refs['form'].resetFields() // 重置表单
            })
          } else {
            return false;
          }
        });
      },
  },
};
</script>

<style scoped>
.form {
  display: block;
  width: 20%;
  position: absolute;
  top: 30%;
  left: 40%;
}
.form .title {
  text-align: center;
  font-size: 1.5em;
  color: white;
  margin-bottom: 20px;
}
.back {
  height: 100vh;
  background-image: linear-gradient(125deg, #e14040, #b050cd, #4842c9, #34828d);
  background-size: 500%;
  animation: animate 20s infinite;
}
@keyframes animate {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}
</style>
